package ui.activity.systemManagement;

import java.util.List;

import ui.activity.ActivityOfAF4Ad;
import ui.viewModel.ModelErrorInfo;
import ui.viewModel.ViewModel;
import ui.viewModel.systemManagement.LoginViewModel;

import com.mysport.ui.R;

import domain.businessEntity.systemManagement.User;
import domain.businessService.systemManagement.ISystemManagementService;
import domain.businessService.systemManagement.SystemManagementService;

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import app.MainActivity;

//登录活动，继承框架中的ActivityOfAF4Ad
public class LoginActivity extends ActivityOfAF4Ad {

	// 声明了一个服务对象，以便于调用服务提供的方法
	private ISystemManagementService loginService;

	// 声明界面中出现的并且跟用户有交互的控件变量
	private Button lg_login;// 登录按钮
	private Button lg_register;// 注册按钮
	private EditText lg_userName;// 登录Login（简称lg_）用户名
	private EditText lg_passWord;// 登录密码
	private TextView more_text;//直接进入主界面

	// 枚举类型，声明了登录后可能出现结果的表示字符串
	public enum LoginResult {
		NotFoundUser, PassWordError, ValidUser;
	}

	// 定义了一个服务对象loginService
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		loginService = new SystemManagementService();// 定义登录服务对象
	}

	// 自动产生，不要动
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}

	// 初始化控件变量，并为相应控件注册事件
	@Override
	protected void initControlsAndRegEvent() {
		// TODO Auto-generated method stub
		// 获取相关控件的ID号
		lg_login = (Button) findViewById(R.id.lg_login);
		lg_register = (Button) findViewById(R.id.lg_register);
		lg_userName = (EditText) findViewById(R.id.lg_userName);
		lg_passWord = (EditText) findViewById(R.id.lg_passWord);
		more_text = (TextView) findViewById(R.id.more_text);

		// 登录按钮事件的响应
		lg_login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 获取ViewModel
				LoginViewModel lvm = (LoginViewModel) vm;

				// 从界面控件中获取数据
				lvm.setUserName(lg_userName.getText().toString());
				lvm.setPassWord(lg_passWord.getText().toString());
				LoginResult lr = doLogin();

				// 根据获取的结果标记，给出相应的提示信息或响应
				if (lr == LoginResult.NotFoundUser) {

					Toast.makeText(getApplication(), "不存在用户", Toast.LENGTH_LONG)
							.show();
				} else {
					if (lr == LoginResult.PassWordError) {
						Toast.makeText(getApplication(), "密码错误",
								Toast.LENGTH_LONG).show();
					} else {
						toMainActivity();
					}
				}
			}
		});

		// 注册按钮事件的响应
		lg_register.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LoginActivity.this,
						RegisterActivity.class);
				startActivity(intent);
			}
		});
		
		//注册直接进入主界面响应事件
		more_text.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				toMainActivity();
			}
		});
	}
	

	// 向主界面跳转的函数
	private void toMainActivity() {
		toActivity(this, MainActivity.class);
	}

	// 中间辅助函数，针对用户各种输入信息产生回馈消息
	public LoginResult doLogin() {
		LoginResult lr = LoginResult.NotFoundUser;
		LoginViewModel lvm = (LoginViewModel) vm;
		User user = loginService.login(lvm.getUserName());

		// 测试用，假数据
		// User user=new User();
		// user.setUsername("a");
		// user.setPassWord("a");
		// Log.i("", "========================"+user.getUsername());
		if (user != null) {
			if (user.getPassWord().equals(lvm.getPassWord())) {
				lr = LoginResult.ValidUser;
			} else {
				lr = LoginResult.PassWordError;
			}
		}
		return lr;
	}

	// 初始化ViewModel
	@Override
	protected ViewModel initModel() {
		// TODO Auto-generated method stub
		LoginViewModel lvm = new LoginViewModel();
		lvm.setUserName("");
		lvm.setPassWord("");
		return lvm;
	}

	// 更新视图View
	@Override
	protected void upDateView(ViewModel aVM) {
		// TODO Auto-generated method stub

	}

	// 处理视图模型ViewModel中出现的错误
	@Override
	protected void processViewModelErrorMsg(List<ModelErrorInfo> errsOfVM,
			String errMsg) {
		// TODO Auto-generated method stub

	}

}
